#include "task_modbus_master.h"
#include "usart.h"
#include "modbus_host.h"

#define ADDR_03H_GAS  0x02 //read register addess
#define ADDR_03H_OTHER  0x20
#define ADDR_03H_GSA_NUM  0x06
#define ADDR_03H_OTHER_NUM  0x07

TaskHandle_t modbus_masterHandler;

void task_modbus_master(void *arg)
{

	int addr_count = 1;
	while (1)
	{

			g_tModH[MODH_USART3_CH1].Rxtimeout = 0;
			MODH_Send03H(MODH_USART3_CH1, addr_count, ADDR_03H_GAS,ADDR_03H_GSA_NUM);
			time_enable();

			//MODH_Send10H(MODH_USART3_CH1, 0x03, _reg, _num, _buf);
		
			

		for (;;)
		{
			if ((g_tModH[MODH_USART3_CH1].RxCount) > 0)
			{
				if ((g_tModH[MODH_USART3_CH1].RxCount) == (g_tModH[MODH_USART3_CH1].Need_RxCount))
				{
					TIM_Cmd(TIM4, DISABLE);
					MODH_Poll(MODH_USART3_CH1);
					g_tModH[MODH_USART3_CH1].RxCount = 0;
					g_tModH[MODH_USART3_CH1].Need_RxCount = 0;
					// g_tModH[MODH_USART3_CH2].Rxtimeout = 0;
					break; //timeout
				}
			}

			if ((g_tModH[MODH_USART3_CH1].Rxtimeout) == 1)
			{

				//g_tModH[MODH_USART3_CH2].RxCount = 0;
				break; //timeout
			}
			vTaskDelay(50);
		}
		
		vTaskDelay(500);
		addr_count ++;
		if(addr_count > 3)
			addr_count = 1;
			
	}
}
